import { SHAPE_COORDS } from './shape';
import { CHESS_TYPE } from './chessType';
cc.Class({
    extends: cc.Component,
    properties: {
        tilePrefab: cc.Prefab,
        bgtilePrefab: cc.Prefab,
        chessPrefab: cc.Prefab,
        shapeContainer: cc.Node,
        shapeBoard: cc.Node,
        restartNode: cc.Node,
        pauseNode: cc.Node,
        pauseResumeBtn: cc.Node,
        pausePic: cc.SpriteFrame,
        resumePic: cc.SpriteFrame,
        scoreLabel: cc.Label,
        RscoreLabel: cc.Label,
        FscoreLabel: cc.Label,

        aimNode: cc.Node,
        bgAudio: {
            default: null,
            type: cc.AudioClip
        },
        btnAudio: {
            default: null,
            type: cc.AudioClip
        },
        dropAudio: {
            default: null,
            type: cc.AudioClip
        },
        pauseResumeAudio: {
            default: null,
            type: cc.AudioClip
        },
        removeAudio: {
            default: null,
            type: cc.AudioClip
        },
        loseAudio: {
            default: null,
            type: cc.AudioClip
        }
    },

    onLoad() {
        // 监听触摸事件
        this.registerEvent_monitor();
        this.registerEvent();
        //表示是否开启疯狂模式，1表示开启，0表示不开启
        this.crazy = cc.sys.localStorage.getItem("Mcrazy");
        // 行列数
        this.row = 15;
        this.col = 10;
        // 块大小
        this.tileWidth = this.shapeBoard.width / this.col;
        this.tileHeight = this.shapeBoard.height / this.row;
        // 功能块产生个数，来确定前部分不产生功能块
        this.frequency = 0;
        // 预示数组
        this.preJuTile = [];
        // 
        this.colorRecover = [];
        // 方块节点池
        this.tilePool = new cc.NodePool();
        // 
        this.tilePool1 = new cc.NodePool();
        // 象棋块节点池
        this.chessPool = new cc.NodePool();
        // 如果是疯狂模式预先上升普通块
        if (this.crazy == 1) {
            this.scheduleOnce(function () {
                this.upAFewMode();
            }, 1)
            this.scheduleOnce(function () {
                this.upAFewMode();
            }, 1)
            this.scheduleOnce(function () {
                this.upAFewMode();
            }, 1)
        }
        // 制作形状
        this.makeShape();
        // 保存已经下落完毕的方块
        this.confirmedTileArray = [];
        // 计时器下降
        this.schedule(this.moveDown, 1);

        this.schedule(this.recoverPreJudge, 0.1);
        this.schedule(this.preJudge, 0.1);

        //if (this.crazy) this.schedule(this.upAFewMode, 5);
        // 用于判断是否已经暂停
        this.isPaused = false;
        // 判断游戏是否结束
        this.end = false;
        // 分数 
        this.score = 0;
        this.loadBg()
        cc.audioEngine.playMusic(this.bgAudio, true);
        cc.audioEngine.setMusicVolume(0.7);
    },
    // 加载背景块
    loadBg() {
        for (let i = 0; i < this.row; i++) {
            for (let j = 0; j < this.col; j++) {
                let keyTile = cc.instantiate(this.bgtilePrefab);
                keyTile.width = this.tileWidth - 1;
                keyTile.height = this.tileHeight - 1;
                this.shapeContainer.addChild(keyTile);
            }
        }
    },
    // 分数计数
    addScore() {
        // 分数+1
        this.score += 1;
        this.scoreLabel.string = "分数：" + String(this.score);
        this.RscoreLabel.string = "分数：" + String(this.score);
        this.FscoreLabel.string = "分数：" + String(this.score);
    },
    // 设置方块颜色
    getColor() {
        // 设置随机颜色
        let red = Math.round(Math.random() * 255);
        let green = Math.round(Math.random() * 255);
        let blue = Math.round(Math.random() * 255);
        return new cc.Color(red, green, blue);
    },
    // 制作方块(包括普通块、功能块)
    makeShape() {
        this.frequency++;
        // 选择方块类型
        this.tileType = this.makeType();
        console.log("type:" + this.tileType);
        this.shapeTileArray = [];                                  // 用来保存当前形状中的所有方块
        // 决定生成块的类型，调整生成块类型的比例,1是普通
        if (this.tileType == 1) {
            this.makeTile()
        }
        else {
            this.makeChess()
        }

    },
    // 选择生成方块类型，1表示普通块，0表示功能块
    makeType() {
        // crazy==1 表示为疯狂模式，只生成功能块
        if (this.crazy == 1) {
            return 0;
        }
        // 前6块只生成普通块
        if (this.frequency <= 6) {
            return 1;
        }
        // 6块后随机产生
        if ((Math.floor(Math.random() * 10)) % 2) {
            return 0;
        }
        else {
            return 1;
        }
    },
    // 制作普通块
    makeTile() {
        // 生成形状
        this.color = this.getColor();                               // 当前形状颜色
        let startX = Math.floor(Math.random() * (this.col - 4)) + 2;      // 横向起始位置
        let startY = 2;                                             // 纵向起始位置
        let x = startX * this.tileWidth;                            // 关键方块x坐标
        let y = startY * this.tileHeight;                           // 关键方块y坐标

        let keyTile = this.getTile();                               // 关键方块(旋转中心点)
        keyTile.color = this.color;
        keyTile.position = cc.v2(x, y);
        keyTile.width = this.tileWidth;
        keyTile.height = this.tileHeight;
        // 加载到棋盘结点显示
        this.shapeBoard.addChild(keyTile);
        // 进入方块数组
        this.shapeTileArray.push(keyTile);
        // 根据中心方块，生成普通块(4个块组成)
        let coords = this.getShapeCoords();                         // 随机获取一个形状坐标
        for (let i = 1; i < coords.length; i++) {
            let x = (coords[i][0] + startX) * this.tileWidth;           // 其他方块相对于关键方块的x坐标
            let y = (coords[i][1] + startY) * this.tileHeight;          // 其他方块相对于关键方块的y坐标
            let tile = this.getTile();                              // 生成方块
            tile.color = this.color;
            tile.position = cc.v2(x, y);
            tile.width = this.tileWidth;
            tile.height = this.tileHeight;
            // 加载到棋盘结点显示
            this.shapeBoard.addChild(tile);
            // 进入方块数组
            this.shapeTileArray.push(tile)
        }
    },
    // 制作功能象棋块
    makeChess() {
        let startX = Math.floor(Math.random() * (this.col - 4)) + 2;      // 横向起始位置
        let startY = 2;                                             // 纵向起始位置
        let x = startX * this.tileWidth;                            // 关键方块x坐标
        let y = startY * this.tileHeight;                           // 关键方块y坐标
        let chess = this.getChess()
        this.chessType = this.getChessType()
        cc.resources.load(this.chessType, cc.SpriteFrame, function (err, ret) {
            if (err) {
                cc.log(err);
                return;
            }
            chess.getComponent(cc.Sprite).spriteFrame = ret;
        }.bind(this));
        chess.position = cc.v2(x, y)
        chess.width = this.tileWidth;
        chess.height = this.tileHeight;
        this.shapeBoard.addChild(chess);
        this.shapeTileArray.push(chess)
    },
    // 生成普通块预制
    getTile() {
        let tile = null;
        if (this.tilePool.size() > 0) {               // 如果节点池中有方块，那从节点池中获取
            tile = this.tilePool.get();
        }
        else {                                        // 否则调用cc.instantiate()生成
            tile = cc.instantiate(this.tilePrefab);
        }
        return tile;
    },
    // 生成功能块预制
    getChess() {
        return cc.instantiate(this.tilePrefab);
    },
    // 随机获取普通块一种形状
    getShapeCoords() {
        let shapeArray = ['squareShape', 'lineShape', 'tShape', 'zShape', 'zShapeMirror', 'lShape', 'lShapeMirror'];
        this.shape = shapeArray[Math.floor(Math.random() * shapeArray.length)];
        // 随机获取该形状的某种形态，形态的索引保存在this.num中
        let coordsArray = SHAPE_COORDS[this.shape];
        this.num = Math.floor(Math.random() * coordsArray.length);
        // 返回坐标
        return coordsArray[this.num];
    },
    // 随机获取功能块一种形状
    getChessType() {
        let chessArray = ['horse', 'elephant', 'handsome', 'scholar', 'car', 'cannon', 'soldier']
        return chessArray[Math.floor(Math.random() * chessArray.length)];
    },
    // 左移操作
    leftBtn() {
        // 如果暂停或结束不再启用
        if (this.isPaused || this.end)
            return;
        // 预示及恢复预示操作
        this.recoverPreJudge();
        this.preJudge();
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            let x = Math.round(this.shapeTileArray[i].x - this.tileWidth);
            let y = Math.round(this.shapeTileArray[i].y);
            // 防止出界
            if (x < 0) {
                return;
            }
            // 如果与其他方块重合，则不能移动
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x);
                let confirmedY = Math.round(this.confirmedTileArray[j].y);
                if (confirmedX == x && confirmedY == y) {
                    return;
                }
            }
        }

        // 当前形状中的方块全部左移一步
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            this.shapeTileArray[i].x -= this.tileWidth;
        }
    },
    // 下落操作
    dropBtn() {
        // 如果暂停或结束不再启用
        if (this.isPaused || this.end)
            return;
        cc.audioEngine.playEffect(this.dropAudio, false);
        // 下落
        while (true) {
            let temp = this.moveDown();
            if (!temp)
                return;
        }
    },
    // 单格下降操作
    moveDown() {
        // 如果暂停或结束不再启用
        if (this.isPaused || this.end)
            return;
        // 预示及恢复预示操作
        this.recoverPreJudge();
        this.preJudge();
        // 往下移动一步
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            let x = Math.round(this.shapeTileArray[i].x);
            let y = Math.round(this.shapeTileArray[i].y - this.tileHeight);
            // 如果触底，则不再下降
            if (Math.abs(y) >= (this.shapeBoard.height)) {
                this.shapeTileArray.forEach(element => {
                    if (this.tileType == 1) {
                        this.confirmedTileArray.push(element);
                    }                // 将确定的方块放入this.confirmedTileArray
                    else {
                        this.chessPool.put(element);
                    }
                });
                // 功能块消除
                if (this.tileType == 0) {
                    this.chessRemove()
                }
                // 疯狂模式上涨
                if (this.crazy == 1) {
                    this.scheduleOnce(function () {
                        this.upAFewMode();
                    }, 1)
                }
                this.makeShape();                                       // 重新生成形状
                return false;
            }
            // 如果与其他方块重合，则不再下降
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x);
                let confirmedY = Math.round(this.confirmedTileArray[j].y);
                if (confirmedX == x && confirmedY == y) {
                    this.shapeTileArray.forEach(element => {            // 将确定的方块放入this.confirmedTileArray
                        if (this.tileType == 1) {
                            this.confirmedTileArray.push(element);
                        }                // 将确定的方块放入this.confirmedTileArray
                        else {
                            this.chessPool.put(element);
                        }
                    });
                    // 判断失败
                    if (this.judgeLose()) {
                        this.lose();                                    // 游戏失败
                    }
                    // 功能块消除
                    if (this.tileType == 0) {
                        this.chessRemove()
                    }
                    // 疯狂模式上涨
                    if (this.crazy == 1) {
                        this.scheduleOnce(function () {
                            this.upAFewMode();
                        }, 1)
                    }
                    this.makeShape();                                   // 没输则重新生成形状
                    return false;
                }
            }
        }
        // 都没有问题的话，则当前形状中的方块全部下降
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            this.shapeTileArray[i].y -= this.tileHeight;
        }
        return true;
    },
    // 右移操作
    rightBtn() {
        // 如果暂停或结束不再启用
        if (this.isPaused || this.end)
            return;
        // 预示及恢复预示操作
        this.recoverPreJudge();
        this.preJudge();
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            let x = Math.round(this.shapeTileArray[i].x + this.tileWidth);
            let y = Math.round(this.shapeTileArray[i].y);
            // 防止出界
            if (x >= this.shapeBoard.width) {
                return;
            }
            // 如果与其他方块重合，则不能移动
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x);
                let confirmedY = Math.round(this.confirmedTileArray[j].y);
                if (confirmedX == x && confirmedY == y) {
                    return;
                }

            }
        }

        // 当前形状中的方块全部右移一步
        for (let i = 0; i < this.shapeTileArray.length; i++) {
            this.shapeTileArray[i].x += this.tileWidth;
        }
    },
    // 旋转操作
    rotateBtn() {
        // 如果暂停或结束不再启用
        if (this.isPaused || this.end)
            return;
        // 如果形状只有一种变化形式，则直接返回
        let temp = this.num;
        if (SHAPE_COORDS[this.shape].length == 1)
            return;
        else {
            if (this.num + 1 == SHAPE_COORDS[this.shape].length)
                this.num = 0;
            else
                this.num += 1;
        }

        let keyTile = this.shapeTileArray[0];               // 获取关键方块
        let coords = SHAPE_COORDS[this.shape][this.num];    // 获取旋转后的坐标
        // 如果旋转后会超出边界或者与其他已存在的方块重合，则不旋转
        for (let i = 1; i < coords.length; i++) {
            let x = Math.round(keyTile.x + coords[i][0] * this.tileWidth);
            let y = Math.round(keyTile.y + coords[i][1] * this.tileHeight);
            // 是否超出边界
            if (x < 0 || x >= this.shapeBoard.width || Math.abs(y) >= this.shapeBoard.height) {
                this.num = temp;
                return;
            }
            // 如果与其他方块重合，则不旋转
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x);
                let confirmedY = Math.round(this.confirmedTileArray[j].y);
                if (confirmedX == x && confirmedY == y) {
                    this.num = temp;
                    return;
                }
            }
        }
        // 根据坐标重新设置其他三个方块
        for (let i = 1; i < coords.length; i++) {
            let x = coords[i][0] * this.tileWidth + keyTile.x;
            let y = coords[i][1] * this.tileHeight + keyTile.y;
            let tile = this.shapeTileArray[i];
            tile.position = cc.v2(x, y);
        }
    },
    // 功能块消除
    chessRemove() {
        this.dropblock = [];
        cc.audioEngine.playEffect(this.removeAudio, false);
        // 选择该功能块
        let x = Math.round(this.shapeTileArray[0].x);
        let y = Math.round(this.shapeTileArray[0].y);
        // 选择消除的形状
        for (let k = 0; k < CHESS_TYPE[this.chessType].length; k++) {
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x + CHESS_TYPE[this.chessType][k][0] * this.tileWidth);
                let confirmedY = Math.round(this.confirmedTileArray[j].y + CHESS_TYPE[this.chessType][k][1] * this.tileHeight);
                if (confirmedX == x && confirmedY == y) {
                    this.particleLoc = { x: this.confirmedTileArray[j].x + this.tileWidth / 2, y: this.confirmedTileArray[j].y - this.tileHeight / 2 };
                    this.dropblock.push([this.confirmedTileArray[j].x, this.confirmedTileArray[j].y]);
                    this.tilePool1.put(this.confirmedTileArray[j]);
                    this.confirmedTileArray.splice(j, 1);
                    this._onTouchBegin();
                    this.addScore()
                    break;
                }
            }
        }
        if (this.dropblock.length != 0) {
            this.updropflag = -1;
            this.scheduleOnce(function () {
                this.dropConfirmedTiles();
            }, 0.5)

        }
    },
    // 消除后的下降
    dropConfirmedTiles() {
        for (let i = 0; i < this.dropblock.length; i++) {
            let x = Math.round(this.dropblock[i][0]);
            let y = Math.round(this.dropblock[i][1]);
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let confirmedX = Math.round(this.confirmedTileArray[j].x);
                let confirmedY = Math.round(this.confirmedTileArray[j].y);
                if (x == confirmedX && confirmedY > y) {
                    this.confirmedTileArray[j].y = this.confirmedTileArray[j].y + this.updropflag * this.tileHeight;

                }
            }
        }
        //cc.audioEngine.playEffect(this.removeAudio, false);
    },
    // 判断是否游戏结束
    judgeLose() {
        for (let i = 0; i < this.confirmedTileArray.length; i++) {
            let confirmedY = Math.round(this.confirmedTileArray[i].y);
            // 如果有任何一个方块超出顶端，则输
            if (confirmedY >= 0)
                return true;
        }
        return false;
    },
    // 游戏失败处理
    lose() {
        // 游戏失败
        this.unschedule(this.moveDown);             // 取消计时器
        this.end = true;
        cc.audioEngine.stopMusic();                 // 停止播放背景音乐
        cc.audioEngine.playEffect(this.loseAudio);  // 播放失败音效
        this.restartNode.active = true;             // 显示restart按钮
    },
    // 游戏暂停处理
    pauseResume() {
        // 游戏结束不可暂停
        if (this.end) {
            return;
        }
        // 游戏暂停
        if (!this.isPaused) {
            this.isPaused = true;                           // 设置isPaused变量
            this.unschedule(this.moveDown);                 // 取消计时器
            cc.audioEngine.stopMusic();
            let btnBg = this.pauseResumeBtn.children[0];    // 修改按钮背景
            btnBg.getComponent(cc.Sprite).spriteFrame = this.pasuePic;
            this.pauseNode.active = true;
        }
        else {
            this.isPaused = false;                          // 设置isPaused变量
            this.schedule(this.moveDown, 1);                // 取消计时器
            cc.audioEngine.playMusic(this.bgAudio, true);
            cc.audioEngine.setMusicVolume(0.7);
            let btnBg = this.pauseResumeBtn.children[0];    // 修改按钮背景
            btnBg.getComponent(cc.Sprite).spriteFrame = this.resumePic;
            this.pauseNode.active = false;
        }
    },
    // 重新开始
    restart() {
        cc.audioEngine.playEffect(this.btnAudio, false);
        cc.director.loadScene('Game');
    },
    changeSence() {
        cc.director.loadScene('Game');
    },
    changeMainSence() {
        cc.director.loadScene('Main');
    },
    start() {
    },
    _onTouchBegin: function () {

        let tempClick = this.particleLoc;

        // console.log("jjj");
        this.newClickNode(tempClick, function (node) {

            if (!node) {

                return
            }

            //杀死所有存在的粒子，然后重新启动粒子发射器。
            node.getComponent(cc.ParticleSystem).resetSystem();
            this.shapeBoard.children.forEach(element => {

                if (element.name === 'clickNode') {

                    //获取粒子系统组件
                    let particle = element.getComponent(cc.ParticleSystem);

                    //指示粒子播放是否完毕
                    if (particle.stopped) {
                        //特效播放完毕的节点放入对象池
                        this._clickPool.put(element);
                        cc.log("顺利回收...");
                    }
                }
            });

        }.bind(this));
    },
    //使用对象池动态实例化预制资源
    newClickNode(position, callBack) {
        let newNode = null;

        if (!this._clickPool) {

            //初始化对象池
            this._clickPool = new cc.NodePool();
        }
        if (this._clickPool.size() > 0) {

            //从对象池请求对象
            newNode = this._clickPool.get();
            this.setClickNode(newNode, position, callBack);
        } else {
            // cc.log("hh");
            // 如果没有空闲对象，我们就用 cc.instantiate 重新创建
            cc.loader.loadRes("prefabs/particle_texture", cc.Prefab, function (err, prefab) {
                if (err) {
                    return;
                }

                newNode = cc.instantiate(prefab);
                this.setClickNode(newNode, position, callBack);
            }.bind(this));
        }

    },
    setClickNode(newNode, position, callBack) {
        newNode.name = "clickNode"; //设置节点名称
        newNode.setPosition(position); //设置节点位置
        cc.log(position.x, position.y);
        cc.log(newNode.x, newNode.y);
        this.shapeBoard.addChild(newNode); //将新的节点添加到当前组件所有节点上
        if (callBack) {
            callBack(newNode); //回调节点
        }

    },

    // 进行预示
    preJudge() {
        if (this.tileType == 1)
            return;
        // this.shape = this.chessType;
        let preDeleteBlock = {
            handsome: [[0, 0], [0, 1], [1, 0], [1, 1], [-1, 0], [-1, 1]],
            horse: [[1, 1], [2, 0], [-1, 1], [-2, 0]],
            scholar: [[-1, 0], [1, 0]],
            elephant: [[2, 1], [-2, 1]],
            car: [[0, 0], [0, 1], [0, 2], [0, 3]],
            cannon: [[0, 1], [1, 1], [-1, 1]],
            soldier: [[0, 0]],
        };
        let maxX = this.shapeTileArray[0].x;
        let maxY = -101010;
        for (let j = 0; j < this.confirmedTileArray.length; j++) {
            let confirmedX = Math.round(this.confirmedTileArray[j].x);
            let confirmedY = Math.round(this.confirmedTileArray[j].y);
            if (confirmedX == Math.round(maxX)) {
                if (confirmedY > maxY) {
                    maxY = this.confirmedTileArray[j].y;
                }
            }
        }
        for (let k = 0; k < preDeleteBlock[this.chessType].length; k++) {
            let confirmedX = Math.round(maxX + preDeleteBlock[this.chessType][k][0] * this.tileWidth);
            let confirmedY = Math.round(maxY - preDeleteBlock[this.chessType][k][1] * this.tileHeight);
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                let x = Math.round(this.confirmedTileArray[j].x);
                let y = Math.round(this.confirmedTileArray[j].y);
                if (confirmedX == x && confirmedY == y) {
                    this.colorRecover.push(this.confirmedTileArray[j].color);
                    this.preJuTile.push(this.confirmedTileArray[j]);
                    this.confirmedTileArray[j].width /= 1.5;
                    this.confirmedTileArray[j].height /= 1.5;
                    //this.confirmedTileArray[j].x=this.confirmedTileArray[j].x+0.165*this.width;                    //this.confirmedTileArray[j].y=this.confirmedTileArray[j].y-0.165*this.height;
                    //this.confirmedTileArray[j].y=this.confirmedTileArray[j].y-0.165*this.height;
                    let xx = this.confirmedTileArray[j].x + 0.165 * this.tileWidth;
                    let yy = this.confirmedTileArray[j].y - 0.165 * this.tileHeight;
                    //this.confirmedTileArray[j].position = cc.v2(xx, yy);

                    this.confirmedTileArray[j].opacity = 100;
                }
            }
        }

    },
    // 恢复预示
    recoverPreJudge() {
        for (let k = 0; k < this.preJuTile.length; k++) {
            this.preJuTile[k].width *= 1.5;
            this.preJuTile[k].height *= 1.5;
            this.preJuTile[k].opacity = 255;
            //this.preJuTile[k].x=this.preJuTile[k].x-0.165*this.width;
            //this.preJuTile[k].y=this.preJuTile[k].y+0.165*this.height;
            let xx = this.preJuTile[k].x - 0.165 * this.tileWidth;
            let yy = this.preJuTile[k].y + 0.165 * this.tileHeight;
            //this.preJuTile[k].position = cc.v2(xx, yy);
        }
        this.preJuTile = [];
        this.colorRecover = [];
    },

    makeATile(xpos, ypos) {
        this.color = this.getColor();                    // 当前形状颜色
        let x = xpos * this.tileWidth;                          // 关键方块x坐标
        let y = -1 * ypos * this.tileHeight;                        // 关键方块y坐标
        let keyTile = this.getTile();                 // 关键方块(旋转中心点)
        this.shapeBoard.addChild(keyTile);
        keyTile.color = this.color;
        keyTile.position = cc.v2(x, y);
        keyTile.width = this.tileWidth;
        keyTile.height = this.tileHeight;
        this.confirmedTileArray.push(keyTile);
    },
    // 上涨一行
    upAlineMode() {
        for (let j = 0; j < this.confirmedTileArray.length; j++) {
            this.confirmedTileArray[j].y = this.confirmedTileArray[j].y + this.tileHeight;
        }
        for (let i = 0; i < this.col; i++) {
            this.makeATile(i, 14);
        }
        if (this.judgeLose()) {
            this.lose();                                    // 游戏失败
        }
    },
    // 随机上涨几个
    upAFewMode() {
        let maxUpNum = this.col;
        let upColNum = Math.floor(Math.random() * maxUpNum);
        for (let i = 0; i < upColNum; i++) {
            let upCol = Math.floor(Math.random() * maxUpNum);
            for (let j = 0; j < this.confirmedTileArray.length; j++) {
                if (this.confirmedTileArray[j].x == upCol * this.tileWidth)
                    this.confirmedTileArray[j].y = this.confirmedTileArray[j].y + this.tileHeight;
            }
            this.makeATile(upCol, 14);
        }
        if (this.judgeLose()) {
            this.lose();                                    // 游戏失败
        }
    },
    // 
    registerEvent_monitor() {
        this.node.on(cc.Node.EventType.TOUCH_END, (e) => {
            let startPoint = e.getStartLocation()
            let endPonit = e.getLocation()
            // 起点与终点相减
            let v = endPonit.sub(startPoint)
            let cnt = 1;
            // 转弧度
            let radians = Math.atan2(v.y, v.x)
            // 弧度转角度
            let degrees = cc.misc.radiansToDegrees(radians)
            /** 将角度划分 8 块区域，方便处理，注意恰好 360 度 */
            let index = Math.floor(degrees / 45)
            this.emitEventByIndex(index, cnt)
        }, this)
    },
    // 
    emitEventByIndex(index, t) {
        // 8 方向判断
        if (index === 0 || index === -1) {
            this.aimNode.emit('RIGHT', t)
        } else if (index === 1 || index === 2) {
            this.aimNode.emit('UP')
        } else if (index === -2 || index === -3) {
            this.aimNode.emit('DOWN')
        } else if (index === -4 || index === 3 || index === 4) {
            this.aimNode.emit('LEFT', t)
        } else {
            cc.error(`无此方向${index}`)
        }
    },
    // 
    registerEvent() {
        // touch 脚本传来上下左右事件，上：变形，下：下一个格子，左右：左右移动一个格子
        this.node.on('UP', () => {
            console.log("sjsjsjsjsjsjj")
            this.rotateBtn();
        }, this)

        this.node.on('DOWN', () => {
            this.dropBtn();
        }, this)

        this.node.on('LEFT', (t) => {
            while (t--)
                this.leftBtn();
        }, this)

        this.node.on('RIGHT', (t) => {
            while (t--)
                this.rightBtn();
        }, this)
    },
    // update (dt) {},
});
